#! /usr/bin/sdlbrt

finp$="video.flv"

'- video to animated-gif zxspectrum converter version 0809111825
'- sdlbasic version 0708072157
'- from firstbasic version: 0201310133
'- needs imagemagick, gifsicle and ffmpeg installed - Linux (or Unix) only?
'- gthumb used for previewing
'- important: depending on the video you have, the .gif result may get quite large (150frames~=1mb)
'- temporary files (.png) also take some significant room as well
'- the conversion takes a time around 25 frames per minute on a core2duo 1.8ghz with linux-i386

'- numeric values into strings: empty strings means no command line entering - ffmpeg time set in seconds
fps$="15":timestart$="15":timedura$="7":gifdelay$="10"


xed=128:yed=96
yed2=int(yed*3):xed2=int(xed*.5):if yed/2<128 then:yed2=yed+128:end if
xedq=xed*2:if xedq/2<128 then:xedq=256:end if
setdisplay(xedq,yed*3,32,1):paper(0xCC8844):ink(0):pen(0):cls



'-xed=256:yed=192:setdisplay(xed,yed,32,1):paper(8^8-1):ink(0):pen(0):cls:

ftempref$="_tmpr"

'- shell:  ffmpeg -i teste01.flv -r 4 -s 256x192 -ss 48 -t 5 _tmpr%d.png
shelout$="ffmpeg -i "+finp$
if fps$<>"" then: shelout$=shelout$+" -r "+fps$: end if
shelout$=shelout$+" -s 128x96"
if timestart$<>"" then: shelout$=shelout$+" -ss "+timestart$: end if
if timedura$<>"" then: shelout$=shelout$+" -t "+timedura$: end if
shelout$=shelout$+" "+ftempref$+"%d.png"
setcaption("ffmpeg: converting..."):shell(shelout$)
frst=0

'- counting frames
frm=1
while fileexists(ftempref$+str$(frm)+".png")<>0
  setcaption("counting frames: "+str$(frm)):frm=frm+1
wend

'- finp$="256192.png"

'- zx81 picture converter - Paulo Silva - August '07 - GPL
'-   source pic needs to be png 
'-   result will be converted to png on Linux (or any unix?) with imagemagick installed 
'- bugs: picture overlapping when ysize is different of *8
'- todo: simplify 'filpic' data (hexdump and linear resizing?)

onlgr=0

'  xed=0:yed=0
'open finp$ for input as #1
'   for i=1 to 16:n=readbyte(1):next:for i=1 to 2:n=readbyte(1):next
'     xed=readbyte(1)*256:xed=xed+readbyte(1)
'   for i=1 to 2:n=readbyte(1):next:yed=readbyte(1)*256:yed=yed+readbyte(1)
' close #1




sub filpic(qxx1,qyy1,qstq$)
  yyt=qyy1:xxt=qxx1:tstt$=qstq$
  for qq=0 to len(tstt$)-1
    qu=(((val(mid$(tstt$,qq+1,1)))*255)/4):ink(rgb(qu,qu,qu)):dot(xxt+qq,yyt):next
  end sub

filpic(0,(yed*2)+ 0,"44444444000044444444000000000000444444440000444444440000000000000404040444444444040404044444444444444444444444444444444444444444")
filpic(0,(yed*2)+ 1,"44444444000044444444000000000000444444440000444444440000000000004040404044444444404040404404404444400044444404444444444444000044")
filpic(0,(yed*2)+ 2,"44444444000044444444000000000000444444440000444444440000000000000404040444444444040404044404404444044404440000044444444440444404")
filpic(0,(yed*2)+ 3,"44444444000044444444000000000000444444440000444444440000000000004040404044444444404040404444444440000444440404444440444444444044")
filpic(0,(yed*2)+ 4,"44444444444444444444444444444444000044440000444400004444000044440404040404040404444444444444444444044444440000044444444444440444")
filpic(0,(yed*2)+ 5,"44444444444444444444444444444444000044440000444400004444000044444040404040404040444444444444444444044444444404044444444444444444")
filpic(0,(yed*2)+ 6,"44444444444444444444444444444444000044440000444400004444000044440404040404040404444444444444444440000004440000044440444444440444")
filpic(0,(yed*2)+ 7,"44444444444444444444444444444444000044440000444400004444000044444040404040404040444444444444444444444444444404444444444444444444")
filpic(0,(yed*2)+ 8,"44444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444")
filpic(0,(yed*2)+ 9,"44444044440444444444444444444444444444444444444444444444444444444444444444444444444444444444444444000044444004444400004444000044")
filpic(0,(yed*2)+10,"44440444444044444440444444444044444444444444044444444444444040444444440444404444444444444444444440444004440404444044440440444404")
filpic(0,(yed*2)+11,"44440444444044444444044444440444440000044444044444444444444404444444404444444444444444444444444440440404444404444444440444440044")
filpic(0,(yed*2)+12,"44440444444044444444404444404444444444444400000444000004440000044444044444444444444444444444444440404404444404444400004444444404")
filpic(0,(yed*2)+13,"44440444444044444444044444440444440000044444044444444444444404444440444444404444444404444440044440044404444404444044444440444404")
filpic(0,(yed*2)+14,"44444044440444444440444444444044444444444444044444444444444040444404444444404444444404444440044444000044440000044000000444000044")
filpic(0,(yed*2)+15,"44444444444444444444444444444444444444444444444444444444444444444444444444044444444044444444444444444444444444444444444444444444")
filpic(0,(yed*2)+16,"44444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444")
filpic(0,(yed*2)+17,"44440444400000044400004440000004440000444400004444000044400000444400004440000444400000044000000444000044404444044400000444444404")
filpic(0,(yed*2)+18,"44400444404444444044444444444404404444044044440440444404404444044044440440444044404444444044444440444404404444044444044444444404")
filpic(0,(yed*2)+19,"44040444400000444000004444444044440000444044440440444404400000444044444440444404400000444000004440444444400000044444044444444404")
filpic(0,(yed*2)+20,"40440444444444044044440444440444404444044400000440000004404444044044444440444404404444444044444440440004404444044444044440444404")
filpic(0,(yed*2)+21,"40000004404444044044440444404444404444044444440440444404404444044044440440444044404444444044444440444404404444044444044440444404")
filpic(0,(yed*2)+22,"44440444440000444400004444404444440000444400004440444404400000444400004440000444400000044044444444000044404444044400000444000044")
filpic(0,(yed*2)+23,"44444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444")
filpic(0,(yed*2)+24,"44444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444")
filpic(0,(yed*2)+25,"40444044404444444044440440444404440000444000004444000044400000444400004400000004404444044044440440444404404444040444440440000004")
filpic(0,(yed*2)+26,"40440444404444444004400440044404404444044044440440444404404444044044444444404444404444044044440440444404440440444044404444444044")
filpic(0,(yed*2)+27,"40004444404444444040040440404404404444044044440440444404404444044400004444404444404444044044440440444404444004444404044444440444")
filpic(0,(yed*2)+28,"40440444404444444044440440440404404444044000004440404404400000444444440444404444404444044044440440444404444004444440444444404444")
filpic(0,(yed*2)+29,"40444044404444444044440440444004404444044044444440440404404440444044440444404444404444044404404440400404440440444440444444044444")
filpic(0,(yed*2)+30,"40444404400000044044440440444404440000444044444444000044404444044400004444404444440000444440044444044044404444044440444440000004")
filpic(0,(yed*2)+31,"44444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444")

filpic(0,(yed*2)+64,"44440044440000004444004444000000222244442222433443244434444442244444004444000000444400444400000022224444222244444444444444444444")
filpic(0,(yed*2)+65,"44440044440000004444004444000000222244442222433431334113434434334444004444000000444400444400000022224444222244444444444444444444")
filpic(0,(yed*2)+66,"44444444444444440044004400440044222222224444444442444212444444344444444444444444004400440044004422222222444444444444444444444444")
filpic(0,(yed*2)+67,"44444444444444440044004400440044222222224444444432234213434444344444444444444444004400440044004422222222444444444444444444444444")
filpic(0,(yed*2)+68,"44344344444444444444444444444444444444444444444442244334422442244444444444444444444444444444444444444444444444444444444444444444")
filpic(0,(yed*2)+69,"44244244433444244223442444444324443343444444444424224324344234234444444444444444444444444444444444444444444444444444444444444444")
filpic(0,(yed*2)+70,"44244244442443344223421342234213433443444434433422424424322434424444444444444444444444444444444444444444444444444444444444444444")
filpic(0,(yed*2)+71,"44344344434444344444443444444334434442444334433442244223322342244444444444444444444444444444444444444444444444444444444444444444")
filpic(0,(yed*2)+72,"44343223422432234224422442243224422432343223322342243443422344434444444444444444444444444444444444444444444444444444444444444444")
filpic(0,(yed*2)+73,"42242224222444333223244224422223244324332224222424432222442444424444444444444444444444444444444444444444444444444444444444444444")
filpic(0,(yed*2)+74,"22133442244243342442422222222442244324332444244424222442442424424444444444444444444444444444444444444444444444444444444444444444")
filpic(0,(yed*2)+75,"44344224422443444224422434433224422432343223344442243443422342244444444444444444444444444444444444444444444444444444444444444444")
filpic(0,(yed*2)+76,"34343444344334434224322442243224422422233443344334433443344332234444444444444444444444444444444444444444444444444444444444444444")
filpic(0,(yed*2)+77,"22342444222222422442244224422442322442442442244224424224332444244444444444444444444444444444444444444444444444444444444444444444")
filpic(0,(yed*2)+78,"24242444244224222442222423322214344242442442333323324224424442444444444444444444444444444444444444444444444444444444444444444444")
filpic(0,(yed*2)+79,"34433223344334434224344442243443422443444224433443343443434432234444444444444444444444444444444444444444444444444444444444444444")

for y1=0 to 31:for x1=0 to 127:u=point(x1,(yed*2)+y1):ink(0xFFFFFF-u):dot(x1,(yed*2)+y1+32):next:next
for y1=64 to 79:for x1=0 to 127:u=point(x1,(yed*2)+y1):ink(0xFFFFFF-u):dot(x1,(yed*2)+y1+16):next:next



for frq=1 to frm-1:setcaption("frame "+str$(frq)+"/"+str$(frm-1))
framenm$=ftempref$+str$(frq)+".png"
'- loadimage(framenm$,1):pasteicon(0,0,1)

shell("convert -modulate 100,0 "+framenm$+" _tmpr_m.png")

loadimage("_tmpr_m.png",1): pasteicon(xedq/2,yed*2,1)



for y1=0 to yed-1 step 4:for x1=0 to xed-1 step 4:dfi=65536:fgx=0:fgy=0
  for yq=0 to 7:for xq=0 to 15:cnt=0
    for y2=0 to 3:for x2=0 to 3
      q1=bitwiseand((point(xedq/2+x1+x2,(yed*2)+y1+y2)),255) 
      q2=bitwiseand((point((onlgr*64)+(xq*4)+x2,(yed*2)+64+(yq*4)+y2)),255)
      dq=abs(q2-q1):cnt=cnt+dq
      next:next
    if cnt<dfi then:dfi=cnt:fgx=xq:fgy=yq:end if
    next:next
  for y2=0 to 7:for x2=0 to 7
    c=point((fgx*8)+x2,(yed*2)+(fgy*8)+y2):ink(c):dot((x1*2)+x2,(y1*2)+y2)
  next:next:next:next



'  fout$=finp$+".bmp":grab (1,0,0,xed*2,yed*2):saveimage (fout$,1)
'- unix only (?) - needs imagemagick installed
'  shell("convert "+fout$+" "+fout$+".png"):shell("rm "+fout$)  


grab (1,0,0,xed*2,yed*2):saveimage(ftempref$+str$(frq)+"_out.bmp",1)
shell("convert "+ftempref$+str$(frq)+"_out.bmp _tmpr.gif")

gifdelv=val(gifdelay$)

  if frst<>0 then:
    shell("gifsicle -l=forever -d="+str$(gifdelv)+" _tmpr1.gif _tmpr.gif > _tmpr2.gif")
  else
    shell("gifsicle -l=forever -d="+str$(gifdelv)+" _tmpr.gif > _tmpr2.gif"):frst=1
    end if

shell("rm "+ftempref$+str$(frq)+"_out.bmp "+ftempref$+str$(frq)+".png")
shell("rm _tmpr.gif _tmpr1.gif _tmpr_m.png"):shell("mv _tmpr2.gif _tmpr1.gif")
next
shell("mv _tmpr1.gif "+finp$+".gif"):shell("gthumb "+finp$+".gif")







